﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BLL
{
   public class HoaDonTienDienBLL : DAL.HoaDonTienDienDAL
    {
       DAL.HoaDonTienDienDAL _dal = new DAL.HoaDonTienDienDAL();
        public string sokydien(string id,DateTime ngay)
        {
            string kq = "0";
            string chisocu = "0";
            string chisomoi = "0";
            string sql1 = @"select min(chisocu) from chisodienke where ma_dk='{0}' and ngayghi='{1}' and trangthai=0";
            sql1 = string.Format(sql1, id,ngay);
            DataTable dt1 = ExecuteQuery(sql1);
            if (dt1.Rows.Count > 0)
                chisocu = dt1.Rows[0][0] + "";

            string sql2 = @"select max(chisomoi) from chisodienke where ma_dk='{0}' and ngayghi='{1}' and trangthai=0";
            sql2 = string.Format(sql2, id,ngay);
            DataTable dt2 = ExecuteQuery(sql2);
            if (dt2.Rows.Count > 0)
                chisomoi = dt2.Rows[0][0] + "";

            Int32 sokydien = 0;
            if (chisocu == "" && chisomoi == "")
                sokydien = 0;
            else
                sokydien = Convert.ToInt32(chisomoi) - Convert.ToInt32(chisocu);
            kq = Convert.ToString(sokydien);

            return kq;
        }

        public string tongsotien(string id,string sokydien,float thue)
        {
            float kq = 0;
            Int32 gia = Convert.ToInt32(_dal.select_gia(id));
            Int32 dinhmuc = Convert.ToInt32(_dal.select_dinhmuc(id));
            Int32 _sokydien = Convert.ToInt32(sokydien);
            float _thue = thue;
            if (_sokydien > dinhmuc)
                kq = (_sokydien - dinhmuc) * gia * 2 + dinhmuc * gia;
            else
                kq = _sokydien * gia;
            kq = kq + kq * _thue;
            return Convert.ToString(kq);
        }
        public string create_ID()
        {
            string a = _dal.selec_max();
            int b = Convert.ToInt32(a);
            b++;
            return b.ToString();
        }
       
    }
}
